/*
 * @Project Name: shoplook
 * @File Name: SuitController
 * @Package Name: com.sm.shoplook.controller.customer
 * @Date: 2018/8/14 16:51
 * @Creator: linshixing-1199
 * @line------------------------------
 * @修改人:
 * @修改时间:
 * @修改内容:
 */

package com.sm.shoplook.controller.front;

import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.base.Material;
import com.sm.shoplook.domain.suit.Suit;
import com.sm.shoplook.domain.suit.SuitComment;
import com.sm.shoplook.service.base.MaterialService;
import com.sm.shoplook.service.suit.ApiSuitCommentService;
import com.sm.shoplook.service.suit.ApiSuitScanRecordService;
import com.sm.shoplook.service.suit.ApiSuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitCommentRespVo;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitReqVo;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenpy
 * @desc 套装-接口
 * @date 2018/8/14 16:51
 * @see
 */
@RestController
@RequestMapping("/front/api/suit")
public class ApiSuitController {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiSuitController.class);
	@Autowired
	private ApiSuitService apiSuitService;
	@Autowired
	private ApiSuitScanRecordService apiSuitScanRecordService;
	@Autowired
	private ApiSuitCommentService suitCommentService;
	@Autowired
	private MaterialService materialService;

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 17:13
	 * @Description 生成套装信息
	 * @param suit 套装信息
	 * @return
	 */
	@RequestMapping(value = "/saveSuit", method = RequestMethod.POST)
	public ApiResult<Integer> saveSuit(Suit suit) {
		try {
			return apiSuitService.saveSuit(suit);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/21 17:12
	 * @Description 套装收藏浏览
	 * @param suitReqVo 浏览收藏
	 * @return
	 */
	@RequestMapping(value = "/updateSuit", method = RequestMethod.POST)
	public ApiResult<Integer> updateSuit(ApiSuitReqVo suitReqVo) {
		try {
			return apiSuitScanRecordService.updateSuit(suitReqVo);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	@RequestMapping(value = "/insertSuitComment", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Integer> insertSuitComment(SuitComment suitComment) {
		try {
			return suitCommentService.insertSuitComment(suitComment);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

	@RequestMapping(value = "/selectCommentsBySuitId", method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<Map<String, Object>> selectCommentsBySuitId(Integer suitId) {
		Map<String, Object> resultMap = new HashMap<>(2);
		ApiResult<List<ApiSuitCommentRespVo>> listApiResult = suitCommentService.selectCommentsBySuitId(suitId);
		ApiResult<Integer> suitCommentCount = suitCommentService.getSuitCommentCount(suitId);
		if (listApiResult.getCode() != 1 || suitCommentCount.getCode() != 1) {
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
		resultMap.put("list", listApiResult.getData());
		resultMap.put("count", suitCommentCount.getData());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, resultMap);
	}

	@RequestMapping(value = "/deleteSuit", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult deleteSuit(Integer id) {
		return apiSuitService.deleteSuit(id);
	}

	/**
	 * @param id 套装ID
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 首页
	 */
	@RequestMapping(value = "/previewSuit", method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<Map<String, Object>> previewSuit(Integer id) {
		Map<String, Object> resultMap = new HashMap<>(2);
		LOGGER.info("id: {}", id);
		ApiResult<Suit> suitApiResult = apiSuitService.selectSuitById(id);
		if (ResultMessage.RESULT_SUCCESS_1.getCode().equals(suitApiResult.getCode())) {
			Suit suit = suitApiResult.getData();
			if (suit == null) {
				suit = new Suit();
				resultMap.put("statisData", suit);
				return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, resultMap);
			}
			String materialIds = suit.getMaterialIds();
			String[] materialIdsArray = materialIds.split(",");
			List<Integer> mIds = new ArrayList<>();
			if (ArrayUtils.isNotEmpty(materialIdsArray)) {
				for (String s : materialIdsArray) {
					mIds.add(Integer.parseInt(s));
				}
				ApiResult<List<Material>> materialResult = materialService.selectMaterialsByIds(mIds);
				resultMap.put("materialIds", materialResult.getData());
			}
			resultMap.put("statisData", suit);
		}
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, resultMap);
	}


}
